package com.siboxuetang.need.web.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.siboxuetang.need.vo.ResultBean;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Create Time: 2018年04月26日 15:24
 *
 * @author fin
 */
public class NeedConverter extends AbstractHttpMessageConverter<ResultBean> {

    @Override
    protected boolean supports(Class<?> aClass) {
        return aClass.equals(ResultBean.class);

    }

    @Override
    protected ResultBean readInternal(Class<? extends ResultBean> aClass, HttpInputMessage httpInputMessage) throws HttpMessageNotReadableException {
        return null;
    }

    @Override
    protected void writeInternal(ResultBean resultBean, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
        OutputStream outputStream = httpOutputMessage.getBody();
        JSONObject json = (JSONObject) JSON.toJSON(resultBean);
        outputStream.write(json.toString().getBytes("utf-8"));
    }
}
